package main.java.study.pdf解析;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.rendering.ImageType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ExtractImagesFromPDF {
    public static void main(String[] args) throws IOException {
        File pdfFile = new File("E:\\WeChat Files\\wxid_dwml989ttnvp22\\FileStorage\\File\\2024-10\\KN2403355.pdf");
        PDDocument document = PDDocument.load(pdfFile);
        PDFRenderer pdfRenderer = new PDFRenderer(document);

        for (int page = 0; page < document.getNumberOfPages(); ++page) {
            // Render PDF page as BufferedImage
            BufferedImage image = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
            // Save image to file
            ImageIO.write(image, "PNG", new File("page_" + page + ".png"));
        }

        document.close();
    }
}
